package com.me.study.javaBasic.dataType;

import org.junit.Assert;
import org.junit.Test;

import java.math.BigDecimal;
import java.math.RoundingMode;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

/**
 * BigDecimal 测试：
 *
 * @author ME
 * @date 2021/4/26
 */
public class TestBigDecimal {

    /**
     * double 直接创建
     */
    @Test
    public void add() {
        double d1 = 0.1;
        double d2 = 0.2;
        assertNotEquals("double 直接计算会精度丢失！！！",
                "0.3", Double.toString(d1 + d2));
        assertNotEquals("double 转 BigDecimal 也会精度丢失！！！",
                "0.3", new BigDecimal(d1).add(new BigDecimal(d2)).toPlainString());

        assertEquals("double 转 String 再转 BigDecimal 才是准确的计算！！！",
                "0.3", new BigDecimal(Double.toString(d1)).add(new BigDecimal(Double.toString(d2))).toPlainString());
    }

    /**
     * equals：比较数值和精度
     * compareTo：只比较数值，-1--小于
     */
    @Test
    public void equals() {
        Assert.assertFalse("BigDecimal.equals() 不仅比较数值，还比较精度，所以为 `false`",
                new BigDecimal("25.9").equals(new BigDecimal("25.90")));
    }

    /**
     * compareTo：只比较数值，-1--小于
     */
    @Test
    public void compareTo() {
        assertEquals("BigDecimal 数值比较用 compareTo()，\n" +
                        "-1 -- 小于\n" +
                        " 0 -- 相等\n" +
                        " 1 -- 大于\n",
                0, new BigDecimal("25.9").compareTo(new BigDecimal("25.90")));
        assertEquals("BigDecimal 数值比较用 compareTo()，\n" +
                        "-1 -- 小于\n" +
                        " 0 -- 相等\n" +
                        " 1 -- 大于\n",
                -1, new BigDecimal("25.9").compareTo(new BigDecimal("25.999")));
    }

    /** 加减乘除 */
    @Test
    public void test() {
        BigDecimal decimal = new BigDecimal("10");

        assertEquals("",
                "11", BigDecimal.TEN.add(BigDecimal.ONE).toString());
        assertEquals("",
                "9", BigDecimal.TEN.subtract(BigDecimal.ONE).toString());
        assertEquals("",
                "20", BigDecimal.TEN.multiply(new BigDecimal("2")).toString());
        assertEquals("",
                "5", BigDecimal.TEN.divide(new BigDecimal("2"), 4, RoundingMode.HALF_UP).toString());
    }

    /** 格式化 */
    @Test
    public void format() {
        BigDecimal decimal = new BigDecimal("10.18546245");
        assertEquals("ROUND_HALF_UP 为四舍五入",
                "10.19", decimal.setScale(2, RoundingMode.HALF_UP).toString());
        assertEquals("ROUND_HALF_UP 为四舍五入",
                "10.185", decimal.setScale(3, BigDecimal.ROUND_HALF_UP).toString());

        decimal = new BigDecimal("-10.18546245");
        assertEquals("负数也是四舍五入",
                "-10.19", decimal.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        assertEquals("负数也是四舍五入",
                "-10.185", decimal.setScale(3, BigDecimal.ROUND_HALF_UP).toString());
    }

    @Test
    public void getStringValue() {
        BigDecimal decimal = new BigDecimal("0.00000005148554641076956121994511276767154838481760200726351203835429763013462401");
        assertEquals("toString() 得到的还是科学计数法", "5.148554641076956121994511276767154838481760200726351203835429763013462401E-8",
                decimal.toString());
        assertEquals("toPlainString() 才能得到本身的值", "0.00000005148554641076956121994511276767154838481760200726351203835429763013462401",
                decimal.toPlainString());
    }

}
